package com.ysy.logistics.controller.system;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ysy.common.resp.ResultData;
import com.ysy.common.resp.ReturnCodeEnum;
import com.ysy.logistics.entities.system.SysNotice;
import com.ysy.logistics.entities.system.UserNotice;
import com.ysy.logistics.entities.system.dto.UserNoticeDto;
import com.ysy.logistics.myannotation.OperateLog;
import com.ysy.logistics.service.system.UserNoticeService;
import com.ysy.logistics.util.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author 姚斯羽
 * @date 2025/4/21 10:43
 * @description: 用户消息管理
 */
@Slf4j
@RestController
@RequestMapping(value = "/userNotice",name = "用户消息")
@CrossOrigin
@Tag(name = "用户消息管理",description = "userNotice表的CRUD")
public class UserNoticeController {
    @Resource
    private UserNoticeService userNoticeService;

    @GetMapping("/getNotReadTotal")
    @Operation(summary = "获取未读消息总数",description = "获取未读消息总数")
    public ResultData getNotReadTotal(){
        //构建查询条件
        String userId = StpUtil.getLoginId().toString();
        LambdaQueryWrapper<UserNotice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserNotice::getUserId,userId)
                .eq(UserNotice::getState,false);
        return ResultData.success(userNoticeService.count(queryWrapper));
    }

    @GetMapping("/getNotReadAndDevice")
    @Operation(summary = "获取设备未读消息",description = "获取设备未读消息")
    public ResultData getNotReadAndDevice(){
        //构建查询条件
        String userId = StpUtil.getLoginId().toString();
       List<SysNotice> list = userNoticeService.getByDeviceNoticeUserId(userId);
       return ResultData.success(list);
    }

    @GetMapping("/getNotReadAndStore")
    @Operation(summary = "获取仓库未读消息",description = "获取仓库未读消息")
    public ResultData getNotReadAndStore(){
        //构建查询条件
        String userId = StpUtil.getLoginId().toString();
        List<SysNotice> list = userNoticeService.getByStoreNoticeUserId(userId);
        return ResultData.success(list);
    }

    @GetMapping("/readNotice/{id}")
    @Operation(summary = "读取消息",description = "读取消息")
    @OperateLog(value = "查看消息详情")
    public ResultData readNotice(@PathVariable("id") String noticeId){
        //构建Update
        LambdaUpdateWrapper<UserNotice> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(UserNotice::getNoticeId,noticeId)
                .eq(UserNotice::getUserId,StpUtil.getLoginId().toString())
                .set(UserNotice::getState,true)
                        .set(UserNotice::getReadTime,new Date());
        userNoticeService.update(updateWrapper);
        return ResultData.success();
    }

    @GetMapping("/readNoticeBy/{id}")
    @Operation(summary = "读取消息",description = "读取消息")
    @OperateLog(value = "查看消息详情")
    public ResultData readNoticeBy(@PathVariable("id") String id){
        //构建Update
        LambdaUpdateWrapper<UserNotice> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(UserNotice::getId,id)
                .set(UserNotice::getState,true)
                .set(UserNotice::getReadTime,new Date());
        userNoticeService.update(updateWrapper);
        return ResultData.success();
    }

    @PostMapping("/readListNotice")
    @Operation(summary = "批量读取消息",description = "批量读取消息")
    public ResultData readListNotice(@RequestBody List<SysNotice> notices){
        log.debug("批量读取消息：{}",notices);
        //构建updat
        LambdaUpdateWrapper<UserNotice> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(UserNotice::getNoticeId,notices.stream().map(SysNotice::getId).filter(StringUtils::isNotBlank).toList())
                .eq(UserNotice::getUserId,StpUtil.getLoginId().toString())
                .set(UserNotice::getState,true)
                .set(UserNotice::getReadTime,new Date());
        userNoticeService.update(updateWrapper);
        return ResultData.success();
    }

    @PostMapping("/listByPage")
    @Operation(summary = "分页查询用户消息",description = "分页查询用户消息")
    @OperateLog(value = "查询用户消息")
    public ResultData<Page<UserNoticeDto>> listByPage(@RequestBody PageInfo<UserNoticeDto> pageInfo){
        //填充用户Id
        pageInfo.getCriteria().setUserId(StpUtil.getLoginId().toString());
        return ResultData.success(userNoticeService.listByPage(pageInfo));
    }

    @GetMapping("/removeById/{id}")
    @Operation(summary = "删除消息",description = "删除消息")
    @OperateLog(value = "删除消息")
    public ResultData removeById(@PathVariable("id") String id){
        if (userNoticeService.removeById(id)){
            return ResultData.success();
        }
        return ResultData.fail("消息不存在");
    }

    @PostMapping("/remove")
    @Operation(summary = "批量删除消息",description = "批量删除消息")
    @OperateLog(value = "删除消息")
    public ResultData remove(@RequestBody PageInfo<UserNoticeDto> page){
        userNoticeService.remove(page);
        return ResultData.success("");
    }
}
